<?php

namespace Medicina\BackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Medicina\KernelBundle\Entity\Persona;
use Medicina\BackendBundle\Form\PersonaType;
use Medicina\BackendBundle\Form\PersonaSearchType;

//use Medicina\KernelBundle\Entity\PersonaRepository;

/**
 * Persona controller.
 *
 */
class PersonaController extends Controller {

    /**
     * Lists all Persona entities.
     *
     */
    public function indexAction() {
        $entity = new Persona();
        $searchForm = $this->createForm(new PersonaSearchType(), $entity);

        $personas = new \Doctrine\Common\Collections\ArrayCollection();
        $paginador = $this->get('ideup.simple_paginator');
        $paginador->setItemsPerPage(10);
        $em = $this->getDoctrine()->getEntityManager();

        $session = $this->get('session');
        $session->set('s_isSearch', false);

        $request = $this->getRequest();
        if ($request->getMethod() === 'POST') {
            $searchForm->bindRequest($request);

            if ($searchForm->isValid()) {
                $session->set('s_isSearch', true);
                $searchCriteria = $searchForm->getData();
                $personas = $em->getRepository('MedicinaKernelBundle:Persona')->getPersonaByCriteria($searchCriteria);
            } else {
                $personas = $em->getRepository('MedicinaKernelBundle:Persona')->findAll();
            }
        } else {
            $personas = $em->getRepository('MedicinaKernelBundle:Persona')->findAll();
        }

        $entities = $paginador->paginate($personas)->getResult();

        return $this->render('BackendBundle:Persona:index.html.twig', array(
                    'entities' => $entities,
                    'paginador' => $paginador,
                    'searchForm' => $searchForm->createView(),
                    'delete_form' => $this->createDeleteForm()->createView()
                ));
    }

    /**
     * Finds and displays a Persona entity.
     *
     */
    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Persona')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Persona entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('BackendBundle:Persona:show.html.twig', array(
                    'entity' => $entity,
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Displays a form to create a new Persona entity.
     *
     */
    public function newAction() {
        $entity = new Persona();
        $form = $this->createForm(new PersonaType(), $entity);

        return $this->render('BackendBundle:Persona:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Creates a new Persona entity.
     *
     */
    public function createAction() {
        $entity = new Persona();
        $request = $this->getRequest();
        $form = $this->createForm(new PersonaType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            try {
                $em->persist($entity);
                $em->flush();
                $this->get('session')->setFlash('notice', 'Persona dada de alta con éxito');

                return $this->redirect($this->generateUrl('persona'));
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'Ya existe una persona con el DNI ingresado');
                return $this->render('BackendBundle:Persona:new.html.twig', array(
                            'entity' => $entity,
                            'form' => $form->createView()
                        ));
            }
        }
    }

    /**
     * Displays a form to edit an existing Persona entity.
     *
     */
    public function editAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Persona')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Persona entity.');
        }

        $editForm = $this->createForm(new PersonaType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('BackendBundle:Persona:edit.html.twig', array(
                    'entity' => $entity,
                    'form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Edits an existing Persona entity.
     *
     */
    public function updateAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Persona')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Persona entity.');
        }

        $editForm = $this->createForm(new PersonaType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            try {

                $em->persist($entity);
                $em->flush();

                return $this->redirect($this->generateUrl('persona'));
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'Ya existe una persona con el DNI ingresado');

                return $this->render('BackendBundle:Persona:edit.html.twig', array(
                    'entity' => $entity,
                    'form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
            }
        }
         return $this->render('BackendBundle:Persona:edit.html.twig', array(
                    'entity' => $entity,
                    'form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
        
//        return $this->render('MedicinaBackendBundle:Persona:edit.html.twig', array(
//                    'entity' => $entity,
//                    'edit_form' => $editForm->createView(),
//                    'delete_form' => $deleteForm->createView(),
//                ));
    }

    /**
     * Deletes a Persona entity.
     *
     */
    public function deleteAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('MedicinaKernelBundle:Persona')->find($id);

        if (!$entity) {
            $ $this->get('session')->setFlash('error', 'No se encontro la persona');
        } else {
            try {
                $distinguible = $entity->getApellido() . ', ' . $entity->getNombre();
                $em->remove($entity);
                $em->flush();
                $this->get('session')->setFlash('notice', 'Persona   ' . $distinguible . '  borrada con exito');
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'No se puede borrar la persona ' . $distinguible . ', esta siendo usada por un concurso');
            }
        }

        return $this->redirect($this->generateUrl('persona'));
    }

    private function createDeleteForm() {
        return $this->createFormBuilder()->getForm();
    }

//    private function createDeleteForm($id) {
//        return $this->createFormBuilder(array('id' => $id))
//                        ->add('id', 'hidden')
//                        ->getForm()
//        ;
//    }
}
